<?php

namespace App\Modules\Member\Controllers;

use App\Lib\Facades\FLAuth;
use App\Lib\Utilities;
use App\Modules\Employee\Services\EmployeeService;
use App\Modules\Employee\Services\PortfolioService;
use App\Modules\Employee\Transformers\GetMostPopularStoreTransformer;
use App\Modules\Employee\Transformers\GetPortfolioListTransformer;
use App\Modules\Live\Services\LiveService;
use App\Modules\Member\Services\MemberService;
use App\Modules\Member\Validators\MemberControllerValidator;
use App\Modules\Message\Services\MessageService;
use App\Modules\Order\Services\OrderCommentService;
use App\Modules\Store\Services\StoreService;
use App\Modules\Store\Transformers\GetStoreListTransformer;
use Flower\Core\Http\BaseController;
use Flower\Facades\Flower;
use Illuminate\Http\Request;

class MemberController extends BaseController
{
    private $orderCommentService;
    private $memberService;
    private $employeeService;
    private $storeService;
    private $portfolioService;
    private $messageService;
	private $liveService;
    public function __construct(OrderCommentService $orderCommentService,
                                MemberService $memberService, EmployeeService $employeeService, StoreService $storeService, PortfolioService $portfolioService, MessageService $messageService,LiveService $liveService)
    {
        $this->orderCommentService = $orderCommentService;
        $this->memberService = $memberService;
        $this->employeeService = $employeeService;
        $this->storeService = $storeService;
        $this->portfolioService = $portfolioService;
        $this->messageService = $messageService;
        $this->liveService = $liveService;
    }

    public function getRules()
    {
        return MemberControllerValidator::validator(); // TODO: Change the autogenerated stub
    }

    /**
     * 获取用户个人信息
     * @param Request $request
     * @return mixed
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getMemberInfo(Request $request)
    {
        $res = $this->memberService->getMemberInfo(FLAuth::id());
        if (empty($res)) return $this->error("获取个人信息失败");
        return $this->display($res, "获取个人信息成功");
    }

    /**
     * 获取附近人气店铺信息列表
     * @param Request $request
     * @return mixed
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getMostPopularStore(Request $request)
    {
        $adcode = $request->input('adcode');
        $pageSize = $request->input('pageSize', 10);
        $list = $this->memberService->getMostPopularStore($adcode, $pageSize);
        $list = (new GetMostPopularStoreTransformer($list))->transform();
        $list = Utilities::dealPage($list);
        return $this->display($list, "获取附近人气店铺信息列表成功");
    }

    /**
     * 获取收藏作品集列表
     * @param Request $request
     * @return mixed
     */
    public function getLikePortfolios(Request $request)
    {
        $list = $this->memberService->getLikePortfolios(FLAuth::id(), $request->page_size);
        $list = (new GetPortfolioListTransformer($list, true))->transform();
        $list = Utilities::dealPage($list);
        return $this->display($list, "获取收藏作品集列表成功");
    }

    /**
     * 获取附近网红理发师
     * @param Request $request
     * @return mixed
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getNearbyNetRedList(Request $request)
    {
        $member_id = FLAuth::id();
        $adcode = $request->input('adcode', 370200);
        $pageSize = $request->input('pageSize', 10);
        $res = $this->employeeService->getNearbyNetRedList($member_id, $adcode, $pageSize);
        return $this->display($res, "获取附近网红理发师成功");
    }

    /**
     * 获取关注的理发师列表
     * @param Request $request
     * @return mixed
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getWxLikeBarberList(Request $request)
    {
        $res = $this->employeeService->getLikeBarberList($request->all());
        if (empty($res)) return $this->error("获取关注理发师列表失败");
        return $this->display($res, "获取关注理发师列表成功");
    }


    public function getWxCoinList(Request $request)
    {
        $res = $this->memberService->getCoinList($request->all());
        if (empty($res)) return $this->error("获取用户县令币信息失败");
        return $this->display($res, "获取用户县令币信息成功");
    }

	/**
	 * 是否第一次登录
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 */
    public function isFirst(Request $request){
    	$memberId = FLAuth::id();
    	$result = $this->memberService->isFirstLogin($memberId);
    	return $this->handleApiResult($result);
    }

//	/**
//	 *  用户关注
//	 * @param Request $request
//	 * @return mixed
//	 */
//    public function likeBarber(Request $request){
//    	$res = $this->memberService->likeBarber($request->all());
//    	if(empty($res)) return $this->error('关注失败');
//    	return $this->success('关注成功');
//    }
//
//	/**
//	 * 用户取消关注
//	 * @param Request $request
//	 * @return mixed
//	 */
//	public function unLikeBarber(Request $request){
//		$res = $this->memberService->unLikeBarber($request->all());
//		if(empty($res)) return $this->error('取消关注失败');
//		return $this->success('取消关注成功');
//	}
//


    /**
     * 小程序首页
     * @param Request $request
     * @return mixed
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function homeWxPage(Request $request)
    {

        $list = $this->portfolioService->getPortFolioList($request->store_id, $request->barber_id, $request->page_size, $request->keyword, FLAuth::id());
        $list = (new GetPortfolioListTransformer($list, false, false, FLAuth::id()))->transform();
        $newList['message'] = $this->messageService->getMessageUnReadNumAndOrderNum(FLAuth::id(), config('constant.message.scene.wx'), null, false);


        // 获取作品集列表

        $newList['portfolio'] = Utilities::dealPage($list);
        // 获取店铺列表
        $user_lng = $request->input('longitude', 35);
        $user_lat = $request->input('latitude', 120);
        $orderBy = $request->input('orderBy', 1);
        $pageSize = $request->input('page_size', 10);
        $keywords = $request->input('keywords');
        $adcode = $request->input('adcode', '370200');
        $stores = $this->storeService->getStoreList($user_lng, $user_lat, $adcode, $orderBy, $keywords, $pageSize);
        $isFirstLogin= $this->memberService->isFirstLogin(FLAuth::id());
	    $newList['is_first_login'] = $isFirstLogin['data'];
        $newList['stores'] = (new GetStoreListTransformer($stores))->transformer();
	    $liveNums = $this->storeService->getLiveRoomNums();
	    $newList['live_num'] = config('constant.live_nums_can_see') ? $liveNums : 0;
        // 未读消息和正在进行的订单数量
        return $newList;
    }

    /**
     * 小程序游客首页
     * @param Request $request
     * @return mixed
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function homeWxPageTourist(Request $request)
    {
        $newList['message']['count_is_not_read'] = 0;
        $newList['message']['order_doing'] = 0;
        $list = $this->portfolioService->getPortFolioList($request->store_id, $request->barber_id, $request->page_size, $request->keyword);
        $list = (new GetPortfolioListTransformer($list, false, false))->transform();
        $newList['portfolio'] = Utilities::dealPage($list);
        // 获取店铺列表
        $user_lng = $request->input('longitude', 35);
        $user_lat = $request->input('latitude', 120);
        $orderBy = $request->input('orderBy', 1);
        $pageSize = $request->input('page_size', 10);
        $keywords = $request->input('keywords');
        $adcode = $request->input('adcode', '370200');
        $stores = $this->storeService->getStoreList($user_lng, $user_lat, $adcode, $orderBy, $keywords, $pageSize);
	    $newList['is_first_login'] =0;
        $newList['stores'] = (new GetStoreListTransformer($stores))->transformer();
        $liveNums = $this->storeService->getLiveRoomNums();
        $newList['live_num'] = config('constant.live_nums_can_see') ? $liveNums : 0;
        // 未读消息和正在进行的订单数量
        return $newList;
    }


	/**
	 *  获取当前用户县令币余额
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 */
    public function getMemberCoinBalance(Request $request){
		$result = $this->memberService->getMemberCoinBalance(FLAuth::id());
		return $this->handleApiResult($result);
    }

	/**
	 * 推广送币
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 */
    public function addCoinToMemberBySpread(Request $request){
    	$openId = $request->input('open_id');
    	$result = $this->memberService->addCoinToMemberBySpread($openId);
    	return $this->handleApiResult($result);
    }
}
